package com.example.fragment;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.example.R;
import com.example.model.EventBean;
import com.example.util.Constant;
import com.example.widget.X5WebView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 分红Fragment
 */
public class BonusFragment extends Fragment {

    @BindView(R.id.web_view_bonus)
    X5WebView webView;

    private View rootView;
    private ValueCallback<Uri> uploadFile;
    private ValueCallback<Uri[]> uploadFiles;
    private String loadUrl = Constant.H5_URL + "bonus";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView==null){
            rootView =inflater.inflate(R.layout.fragment_bonus,null,false);
            ButterKnife.bind(this,rootView);
            EventBus.getDefault().register(this);
        }
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initWebView();
    }

    /**
     *
     */
    private void initWebView(){
        webView.getSettings().setUseWideViewPort(true);
        webView.loadUrl(loadUrl);

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                webView.evaluateJavascript("document.getElementsByTagName('body')[0].getElementsByClassName('kz-float-layer bottom')[0].remove();",
                        new ValueCallback<String>() {
                            //加载网页结束之后的
                            @Override
                            public void onReceiveValue(String s) {
                                Log.e("zz", s);
                            }
                        });

            }


            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            //加载进度
            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
                Log.e("zz", String.valueOf(i));
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventBean bean) {
        webView.loadUrl(loadUrl);
        webView.loadUrl( "javascript:window.location.reload( true )" );
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(webView!=null){
            webView.destroy();
        }
        EventBus.getDefault().unregister(this);
    }
}
